//
//  RWLoginView.m
//  RedWine
//
//  Created by Liufangfang on 2016/12/30.
//  Copyright © 2016年 HuiBangKe. All rights reserved.
//

#import "RWLoginView.h"
#import "FTYInfoVertifyTools.h"

#define kLoginActionButtonTagBase 400

@interface RWLoginView ()<UITextFieldDelegate>

@end

@implementation RWLoginView

- (instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
        [self setLoginUI];
    }
    return self;
}

- (void)setLoginUI{

    UIView *line1 = [[UIView alloc] init];
    [self addSubview:line1];
    line1.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
    [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(20.*FTY_HEIGHT_BASE);
        make.left.right.mas_offset(0);
        make.height.mas_offset(.5*FTY_HEIGHT_BASE);
    }];
    
    UIButton *phoneIcon = [[UIButton alloc] init];
    [self addSubview:phoneIcon];
    [phoneIcon setImage:[UIImage imageNamed:@"myiphone_icon"] forState:UIControlStateNormal];
    [phoneIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line1.mas_bottom).offset(15.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(10.*FTY_WIDTH_BASE);
        make.width.height.mas_equalTo(30.*FTY_WIDTH_BASE);
    }];
    
    UITextField *phoneTF = [[UITextField alloc] init];
    [self addSubview:phoneTF];
    phoneTF.placeholder = @"请输入手机号";
//    phoneTF.text = @"18310823591";
    phoneTF.textColor = [FTYColor blackColor];
    phoneTF.font = FTY_FONTTSIZE(15.);
    phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    [phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line1.mas_bottom).offset(20.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(phoneIcon.mas_right).offset(10.*FTY_WIDTH_BASE);
        make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
    }];
    _phoneTF = phoneTF;
    
    UIView *line2 = [[UIView alloc] init];
    [self addSubview:line2];
    line2.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
    [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(phoneIcon.mas_bottom).offset(15.*FTY_HEIGHT_BASE);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(.5*FTY_HEIGHT_BASE);
    }];
    
    UIButton *passwordIcon = [[UIButton alloc] init];
    [self addSubview:passwordIcon];
    [passwordIcon setImage:[UIImage imageNamed:@"code_icon"] forState:UIControlStateNormal];
    [passwordIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line2.mas_bottom).offset(15.*FTY_HEIGHT_BASE);
        make.left.width.height.mas_equalTo(phoneIcon);
    }];
    
    UITextField *passwordTF = [[UITextField alloc] init];
    [self addSubview:passwordTF];
    passwordTF.placeholder = @"请输入密码";
//    passwordTF.text = @"12345678";
    passwordTF.textColor = [FTYColor blackColor];
    passwordTF.font = FTY_FONTTSIZE(15.);
    [passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line2.mas_bottom).offset(20.*FTY_HEIGHT_BASE);
        make.left.width.height.mas_equalTo(phoneTF);
    }];
    _passwordTF = passwordTF;
    
    //手机快速登录显示
    FTYButton *getCodeBtn = [FTYButton createButtonWithTitle:@"获取验证码" tColor:[UIColor whiteColor] fontSize:13*FTY_WIDTH_BASE backGroundColor:[FTYColor getMainColorWithAlpha:1.] cornerRadius:15.*FTY_WIDTH_BASE];
    [passwordTF addSubview:getCodeBtn];
    [getCodeBtn addTarget:self action:@selector(getCodeBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    getCodeBtn.hidden = YES;
    [getCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(passwordTF.mas_centerY);
        make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
        make.width.mas_equalTo(80.*FTY_WIDTH_BASE);
        make.height.mas_equalTo(30.*FTY_HEIGHT_BASE);
    }];
    _getCodeBtn = getCodeBtn;
    
    UIView *line3 = [[UIView alloc] init];
    [self addSubview:line3];
    line3.backgroundColor = [FTYColor getEEEEEEColorWithAlpha:1.];
    [line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(passwordIcon.mas_bottom).offset(15.*FTY_HEIGHT_BASE);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(.5*FTY_HEIGHT_BASE);
    }];
    
    UIButton *registerBtn = [[UIButton alloc] init];
    [self addSubview:registerBtn];
    [registerBtn setTitle:@"注册" forState:UIControlStateNormal];
    [registerBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    registerBtn.titleLabel.font = FTY_FONTTSIZE(15);
    registerBtn.tag = kLoginActionButtonTagBase;
    [registerBtn addTarget:self action:@selector(loginViewButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line3.mas_bottom).offset(15.*FTY_HEIGHT_BASE);
        make.left.mas_equalTo(10.*FTY_WIDTH_BASE);
        make.height.mas_equalTo(30.*FTY_HEIGHT_BASE);
        make.width.mas_equalTo(50.*FTY_WIDTH_BASE);
    }];
    _registerBtn = registerBtn;
    
    UIButton *forgetPasswordBtn = [[UIButton alloc] init];
    [self addSubview:forgetPasswordBtn];
    [forgetPasswordBtn setTitle:@"忘记密码" forState:UIControlStateNormal];
    [forgetPasswordBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    forgetPasswordBtn.titleLabel.font = FTY_FONTTSIZE(15);
    forgetPasswordBtn.tag = kLoginActionButtonTagBase + 1;
    [forgetPasswordBtn addTarget:self action:@selector(loginViewButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [forgetPasswordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(registerBtn.mas_top);
        make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
        make.height.mas_equalTo(30.*FTY_HEIGHT_BASE);
        make.width.mas_equalTo(100.*FTY_WIDTH_BASE);
    }];
    _forgetPasswordBtn = forgetPasswordBtn;
    
    FTYButton *passwordLoginBtn = [FTYButton createButtonWithTitle:@"密码登录" tColor:[UIColor blackColor] fontSize:15.*FTY_WIDTH_BASE backGroundColor:[FTYColor getMainColorWithAlpha:1.] cornerRadius:15.*FTY_WIDTH_BASE borderColor:[FTYColor getEEEEEEColorWithAlpha:1.] borderWidth:.5*FTY_WIDTH_BASE];
    [self addSubview:passwordLoginBtn];
    passwordLoginBtn.tag = kLoginActionButtonTagBase + 2;
    [passwordLoginBtn addTarget:self action:@selector(loginViewButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [passwordLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(line3.mas_bottom).offset(100.*FTY_HEIGHT_BASE);
        make.right.mas_equalTo(-10.*FTY_WIDTH_BASE);
        make.height.mas_equalTo(40.*FTY_HEIGHT_BASE);
        make.width.mas_equalTo(FTY_SCREEN_WIDTH - 10.*FTY_WIDTH_BASE * 2);
    }];
    _passwordLoginBtn = passwordLoginBtn;
 
    UIButton *phoneLoginBtn = [[UIButton alloc] init];
    [self addSubview:phoneLoginBtn];
    phoneLoginBtn.backgroundColor = [UIColor whiteColor];
    [phoneLoginBtn setTitle:@"手机快速登录" forState:UIControlStateNormal];
    [phoneLoginBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    phoneLoginBtn.titleLabel.font = FTY_FONTTSIZE(15);
    phoneLoginBtn.layer.masksToBounds = YES;
    phoneLoginBtn.layer.cornerRadius = 15.*FTY_WIDTH_BASE;
    phoneLoginBtn.layer.borderColor = [FTYColor getEEEEEEColorWithAlpha:1.].CGColor;
    phoneLoginBtn.layer.borderWidth = 1.*FTY_WIDTH_BASE;
    phoneLoginBtn.tag = kLoginActionButtonTagBase + 3;
    [phoneLoginBtn addTarget:self action:@selector(loginViewButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [phoneLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(passwordLoginBtn.mas_bottom).offset(15.*FTY_HEIGHT_BASE);
        make.right.height.width.mas_equalTo(passwordLoginBtn);
    }];
    _phoneLoginBtn = phoneLoginBtn;
}

#pragma mark - <UITextFieldDelegate>

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [_phoneTF resignFirstResponder];
    [_passwordTF resignFirstResponder];
}

#pragma mark - Private Invoke Methods

- (void)getCodeBtnAction:(UIButton *)btn{

    [FTYInfoVertifyTools phoneNumberVertify:_phoneTF.text showAtView:self];
    
    [FTYHTTPManager postInfoWithSubUrl:[NSString stringWithFormat:@"/shop/ybkapi.php?act=ybkapi&op=verify&type=login&member_mobile=%@", _phoneTF.text] parameters:nil block:^(id result, NSError *error) {
        
        if (!error) {
            
            NSInteger code = [[[result objectForKey:@"err"] objectForKey:@"errorcode"] integerValue];
            NSString *info = [[result objectForKey:@"err"] objectForKey:@"errorinfo"];
            if (0 == code) {
                [[FTYToastView alloc] addTitle:@"验证码已发送～" addView:self];
            } else {
                [[FTYToastView alloc] addTitle:info addView:self];
            }
        } else {
            NSLog(@"=====%@", error);
        }
    }];
}

- (void)loginViewButtonAction:(UIButton *)btn{
    
    NSInteger btnTag = btn.tag;
    
    if (402 == btnTag) {
        
        btn.backgroundColor = [FTYColor getMainColorWithAlpha:1.];
        _getCodeBtn.hidden = YES;
        _passwordTF.placeholder = @"请输入密码";
        _passwordTF.keyboardType = UIKeyboardTypeDefault;
        _phoneLoginBtn.backgroundColor =[UIColor whiteColor];
    } else if (403 == btnTag) {

        btn.backgroundColor = [FTYColor getMainColorWithAlpha:1.];
        _getCodeBtn.hidden = NO;
        _passwordTF.placeholder = @"请输入验证码";
        _passwordTF.keyboardType = UIKeyboardTypeNumberPad;
        _passwordLoginBtn.backgroundColor = [UIColor whiteColor];
    }
        
    if (_delegate && [_delegate respondsToSelector:@selector(loginViewAction:)]) {
        [_delegate loginViewAction:btn];
    }
}


@end
